package com.example.framework.security.core;

import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 登录用户信息
 */
@Data
@Accessors(chain = true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginUser implements UserDetails {

    /**
     * 实际上是唯一的用户 id，不能为空
     */
    private String username;

    @JsonIgnore
    private String password;

    /**
     * 不存 redis 中，不能为空
     */
    @JsonIgnore
    private Set<GrantedAuthority> authorities;

    @Override
    public Set<GrantedAuthority> getAuthorities() {
        if (ObjectUtil.isEmpty(permissions)) {
            return Set.of();
        }
        return permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
    }

    /**
     * 权限，用于存redis
     */
    private List<String> permissions;

    /**
     * 用户昵称，非唯一
     */
    private String nickname;

    private String countryId;

    private String deviceId;

}
